﻿package core.ui.controls
{
    import core.enum.*;
    import core.event.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class WindowScroll extends Sprite
    {
        private var type:Direction;
        private var size:int;
        private var csize:int;
        private var csdiffer:int;
        private var samt:int;
        private var home:Sprite;
        private var end:Sprite;
        private var drag:Sprite;
        private var bg:Sprite;
        private var dstart:Bitmap;
        private var dmiddle:Bitmap;
        private var dstop:Bitmap;
        private var dgrip:Bitmap;
        public var halt:Boolean = false;

        public function WindowScroll(param1:Direction, param2:int, param3:int, param4:int = 0, param5:int = 0)
        {
            this.type = param1;
            this.size = param2;
            this.samt = param5;
            this.csize = param3;
            this.csdiffer = param4;
            if (param1 == Direction.Vertical)
            {
                this.RenderVertical();
            }
            else
            {
                this.RenderHorizontal();
            }
            return;
        }// end function

        public function SetScrollDiffer(csize:int) : void
        {
            this.csdiffer = csize;
            this.Recalculate();
            return;
        }// end function

        public function SetScrollAmount(csize:int) : void
        {
            this.samt = csize;
            this.Recalculate(true);
            return;
        }// end function

        public function SetContentSize(csize:int) : void
        {
            this.csize = csize;
            this.Recalculate();
            return;
        }// end function

        public function SetSize(csize:int) : void
        {
            this.size = csize;
            this.Recalculate();
            return;
        }// end function

        public function Recalculate(csize:Boolean = false) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (this.type == Direction.Vertical)
            {
                if (!this.halt && this.csize > this.size + this.csdiffer)
                {
                    if (this.home.visible == false)
                    {
                        this.home.visible = true;
                        this.end.visible = true;
                        this.drag.visible = true;
                    }
                    if (!csize)
                    {
                        _loc_2 = int(this.size - 42 - (this.csize - this.size - this.csdiffer) / 2);
                        this.dmiddle.height = _loc_2 < 8 ? (8) : (_loc_2);
                        this.dstop.y = this.dmiddle.y + this.dmiddle.height;
                        this.dgrip.y = this.dmiddle.y + int(this.dmiddle.height / 2) - 3;
                        this.end.y = this.size - 15;
                    }
                    this.drag.y = Math.round(this.samt / (this.csize - this.size) * (this.size - this.drag.height - 32)) + 16;
                }
                else if (this.home.visible == true)
                {
                    this.home.visible = false;
                    this.end.visible = false;
                    this.drag.visible = false;
                }
                this.bg.height = this.size;
            }
            else
            {
                if (!this.halt && this.csize > this.size + this.csdiffer)
                {
                    if (this.home.visible == false)
                    {
                        this.home.visible = true;
                        this.end.visible = true;
                        this.drag.visible = true;
                    }
                    if (!csize)
                    {
                        _loc_3 = int(this.size - 42 - (this.csize - this.size - this.csdiffer) / 2);
                        this.dmiddle.width = _loc_3 < 8 ? (8) : (_loc_3);
                        this.dstop.x = this.dmiddle.x + this.dmiddle.width;
                        this.dgrip.x = this.dmiddle.x + int(this.dmiddle.width / 2) - 4;
                        this.end.x = this.size - 15;
                    }
                    this.drag.x = Math.round(this.samt / (this.csize - this.size - this.csdiffer) * (this.size - this.drag.width - 32)) + 16;
                }
                else if (this.home.visible == true)
                {
                    this.home.visible = false;
                    this.end.visible = false;
                    this.drag.visible = false;
                }
                this.bg.width = this.size;
            }
            return;
        }// end function

        private function RenderVertical() : void
        {
            this.bg = new Sprite();
            this.bg.graphics.beginFill(14211289);
            this.bg.graphics.drawRect(0, 0, 16, 16);
            this.bg.graphics.endFill();
            this.bg.height = this.size;
            addChild(this.bg);
            this.home = new Sprite();
            this.home.addChild(new Bitmap(new ScrollbarUp()));
            this.home.addEventListener(MouseEvent.MOUSE_DOWN, this.UpClick);
            var _loc_1:int = 1;
            this.home.y = 1;
            this.home.x = _loc_1;
            addChild(this.home);
            this.end = new Sprite();
            this.end.addChild(new Bitmap(new ScrollbarDown()));
            this.end.addEventListener(MouseEvent.MOUSE_DOWN, this.DownClick);
            this.end.x = 1;
            this.end.y = this.size - 15;
            addChild(this.end);
            this.drag = new Sprite();
            this.drag.addEventListener(MouseEvent.MOUSE_DOWN, this.DragMouseDown);
            this.drag.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp);
            this.drag.x = 1;
            this.drag.y = 16;
            this.dstart = new Bitmap(new ScrollbarVdrgTop());
            this.drag.addChild(this.dstart);
            this.dmiddle = new Bitmap(new ScrollbarVdrgMiddle());
            this.dmiddle.y = 5;
            this.drag.addChild(this.dmiddle);
            this.dstop = new Bitmap(new ScrollbarVdrgBottom());
            this.dstop.y = 15;
            this.drag.addChild(this.dstop);
            this.dgrip = new Bitmap(new ScrollbarVdrgGrip());
            this.dgrip.y = 6;
            this.dgrip.x = 5;
            this.drag.addChild(this.dgrip);
            addChild(this.drag);
            this.Recalculate();
            return;
        }// end function

        private function RenderHorizontal() : void
        {
            this.bg = new Sprite();
            this.bg.graphics.beginFill(14211289);
            this.bg.graphics.drawRect(0, 0, 16, 16);
            this.bg.graphics.endFill();
            this.bg.width = this.size;
            addChild(this.bg);
            this.home = new Sprite();
            this.home.addChild(new Bitmap(new ScrollbarLeft()));
            this.home.addEventListener(MouseEvent.MOUSE_DOWN, this.LeftClick);
            var _loc_1:int = 1;
            this.home.y = 1;
            this.home.x = _loc_1;
            addChild(this.home);
            this.end = new Sprite();
            this.end.addChild(new Bitmap(new ScrollbarRight()));
            this.end.addEventListener(MouseEvent.MOUSE_DOWN, this.RightClick);
            this.end.y = 1;
            this.end.x = this.size - 15;
            addChild(this.end);
            this.drag = new Sprite();
            this.drag.addEventListener(MouseEvent.MOUSE_DOWN, this.DragMouseDown);
            this.drag.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp);
            this.drag.y = 1;
            this.drag.x = 16;
            this.dstart = new Bitmap(new ScrollbarHdrgLeft());
            this.drag.addChild(this.dstart);
            this.dmiddle = new Bitmap(new ScrollbarHdrgMiddle());
            this.dmiddle.x = 5;
            this.drag.addChild(this.dmiddle);
            this.dstop = new Bitmap(new ScrollbarHdrgRight());
            this.dstop.x = 15;
            this.drag.addChild(this.dstop);
            this.dgrip = new Bitmap(new ScrollbarHdrgGrip());
            this.dgrip.x = 6;
            this.dgrip.y = 5;
            this.drag.addChild(this.dgrip);
            addChild(this.drag);
            this.Recalculate();
            return;
        }// end function

        private function EnterFrame(event:Event) : void
        {
            if (this.type == Direction.Vertical)
            {
                dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL, (this.csize - this.size - this.csdiffer) * ((this.drag.y - 16) / (this.size - this.drag.height - 32))));
            }
            else
            {
                dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL, (this.csize - this.size - this.csdiffer) * ((this.drag.x - 16) / (this.size - this.drag.width - 32))));
            }
            return;
        }// end function

        function UpClick(event:MouseEvent) : void
        {
            if (this.drag.y - 10 > 16)
            {
                this.drag.y = this.drag.y - 10;
            }
            else
            {
                this.drag.y = 16;
            }
            this.EnterFrame(null);
            return;
        }// end function

        function DownClick(event:MouseEvent) : void
        {
            if (this.drag.y + 10 < this.size - this.drag.height - 16)
            {
                this.drag.y = this.drag.y + 10;
            }
            else
            {
                this.drag.y = this.size - this.drag.height - 16;
            }
            this.EnterFrame(null);
            return;
        }// end function

        function LeftClick(event:MouseEvent) : void
        {
            if (this.drag.x - 10 > 16)
            {
                this.drag.x = this.drag.x - 10;
            }
            else
            {
                this.drag.x = 16;
            }
            this.EnterFrame(null);
            return;
        }// end function

        function RightClick(event:MouseEvent) : void
        {
            if (this.drag.x + 10 < this.size - this.drag.width - 16)
            {
                this.drag.x = this.drag.x + 10;
            }
            else
            {
                this.drag.x = this.size - this.drag.width - 16;
            }
            this.EnterFrame(null);
            return;
        }// end function

        function DragMouseDown(event:MouseEvent) : void
        {
            if (this.type == Direction.Vertical)
            {
                this.drag.startDrag(false, new Rectangle(1, 16, 0, this.size - 32 - this.drag.height));
            }
            else
            {
                this.drag.startDrag(false, new Rectangle(16, 1, this.size - 32 - this.drag.width, 0));
            }
            this.addEventListener(Event.ENTER_FRAME, this.EnterFrame);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, true);
            return;
        }// end function

        function DragMouseUp(event:MouseEvent) : void
        {
            this.drag.stopDrag();
            this.removeEventListener(Event.ENTER_FRAME, this.EnterFrame);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, true);
            return;
        }// end function

    }
}
